﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using StartEAS_API.Utils;
using StartsEAS_API.BLL.Interfaces.IScheduleB;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;

namespace StartsEAS_API.Controllers.ScheduleContor
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [EnableCors("any")]
    public class ScheduleController : ControllerBase
    {
        private readonly IScheduleBLL _scheduleBll;

        public ScheduleController(IScheduleBLL ScheduleBll)
        {
            _scheduleBll = ScheduleBll;
        }
        /// <summary>
        /// 添加课表
        /// </summary>
        /// <param name="data">课表JSON数据</param>
        /// <returns></returns>
        [HttpPost]
        public ResultUtils InsertCourse(JArray data)
        {
            try
            {
                return _scheduleBll.InsertSchedule(data);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"InsertCourse服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 根据班级编号查询课表，一般供教务处进行课表查询
        /// </summary>
        /// <param name="classId">班级编号</param>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetSchedultByClassId(string classId)
        {
            try
            {
                return _scheduleBll.GetSchedultByClassId(classId);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetSchedultByClassId服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 根据上课教师进行查询课表,一般不作使用
        /// </summary>
        /// <param name="token">token</param>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetSchedultByWorkerId(string token)
        {
            try
            {
                return _scheduleBll.GetSchedultByWorkerId(token);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetSchedultByWorkerId服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 根据上课教室进行查询课表
        /// </summary>
        /// <param name="SchedulePlace">上课教室</param>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetSchedultBySchedulePlace(string SchedulePlace)
        {
            try
            {
                return _scheduleBll.GetSchedultBySchedulePlace(SchedulePlace);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetSchedultBySchedulePlace服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 根据Token查询课表，平时教师和学生均使用该接口
        /// </summary>
        /// <param name="token">Token</param>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetSchedultByToken(string token) {
            try
            {
                return _scheduleBll.GetSchedultByToken(token);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetSchedultByToken服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 查询学生需要教评的课程
        /// </summary>
        /// <param name="token">学生token</param>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetWorkerEvaluationByStudent(string token, int Semester) {
            try
            {
                return _scheduleBll.GetWorkerEvaluationByStudent(token, Semester);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetWorkerEvaluationByStudent服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 调课
        /// </summary>
        /// <param name="data">调课信息</param>
        /// <returns></returns>
        [HttpPut]
        public ResultUtils AdjustTheCourse(Schedule data)
        {
            try
            {
                return _scheduleBll.AdjustTheCourse(data);
            }
            catch (System.Exception e) {
                AppLog.WriteError($"AdjustTheCourse服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
    }
}
